package com.xiaoye.qbproject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoye.qbproject.domain.ZbRelation;
import com.xiaoye.qbproject.mapper.ZbRelationMapper;
import com.xiaoye.qbproject.service.ZbRelationService;
import com.xiaoye.qbproject.vo.ZbRelationVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【ZB_RELATION(装备实体关系表)】的数据库操作Service实现
 * @createDate 2024-12-28 14:38:08
 */
@Service
public class ZbRelationServiceImpl extends ServiceImpl<ZbRelationMapper, ZbRelation>
        implements ZbRelationService {
    /**
     * @param toId 上级节点
     * @description: 获取装备实体下被指向的节点信息
     * @author: ChenYao
     * @return: java.util.List<com.xiaoye.qbproject.domain.ZbRelation>
     * @ceateDate: 2024-12-28 14:54
     */

    @Override
    public List<ZbRelation> getFromZbRelationList(String toId) {
        return baseMapper.selectList(Wrappers.<ZbRelation>lambdaQuery().eq(ZbRelation::getToId, toId));
    }

    /**
     * @param zbRelationVo
     * @description: 设置实体关系
     * @author: ChenYao
     * @return: void
     * @ceateDate: 2024-12-28 15:03
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addZbRelation(ZbRelationVo zbRelationVo) {
        // 删除旧的实体关系
/*        baseMapper.delete(
                Wrappers.<ZbRelation>lambdaQuery()
                        .eq(ZbRelation::getToId, zbRelationVo.getToId()));*/

        String toId = zbRelationVo.getToId();
        String text = zbRelationVo.getText();
        List<String> fromIds = zbRelationVo.getFromIds();
        // 添加新的实体关系
        fromIds.forEach(id -> baseMapper.insert(new ZbRelation(toId, id, text)));

    }

    @Override
    public void editZbRelation(ZbRelationVo zbRelationVo) {
        baseMapper.delete(
                Wrappers.<ZbRelation>lambdaQuery()
                        .eq(ZbRelation::getToId, zbRelationVo.getToId()));
        String toId = zbRelationVo.getToId();
        String text = zbRelationVo.getText();
        List<String> fromIds = zbRelationVo.getFromIds();
        // 添加新的实体关系
        fromIds.forEach(id -> baseMapper.insert(new ZbRelation(toId, id, text)));
    }
}




